package cn.yanhu.kuwanapp.utils

import android.content.Context
import android.view.View
import cn.woochen.comlib.util.ConfigPref
import cn.woochen.comlib.util.UserPref
import cn.yanhu.kuwanapp.common.activity.CommonWebviewActivity
import cn.yanhu.kuwanapp.event.AccountLoginEvent
import cn.yanhu.kuwanapp.event.VideoRewardEvent
import cn.yanhu.kuwanapp.home.MainActivity
import cn.yanhu.kuwanapp.my.activity.*
import cn.yanhu.kuwanapp.utils.constans.Constants
import cn.yanhu.kuwanapp.utils.ext.logcom
import cn.yanhu.kuwanapp.utils.ext.showToast
import cn.yanhu.kuwanapp.utils.helper.ServiceHelper
import cn.yanhu.kuwanapp.welfare.VideoActivity
import cn.yanhu.kuwanapp.welfare.WelfareViewModel
import cn.yanhu.kuwanapp.welfare.ad.AdPlatformFactory
import cn.yanhu.kuwanapp.welfare.ad.AdPlatformType
import cn.yanhu.kuwanapp.welfare.ad.AdSlotType
import cn.yanhu.kuwanapp.welfare.ad.IAdProcess
import cn.yanhu.kuwanapp.welfare.ad.callback.SimpleAdLifecycle
import com.app.miaoaiwan.common.ad.server.AdStatusUpload
import com.jeremyliao.liveeventbus.LiveEventBus
import jfq.wowan.com.myapplication.PlayMeUtil

/**
 *类描述：导航帮助类
 *@author woochen
 *@time 2021/8/5 12:17
 */
object NavigatorHelper {

   private const val pagePre = "KU_WAN_PAGE_INDEX/?"
    private var mUserId by UserPref(Constants.USER_ID, "")
    private var mImei by UserPref(Constants.IMEI, "")
    private var mDeviceOaid by ConfigPref(Constants.DEVICE_OAID, "")

    /**
     * 跳转cpl
     */
    fun toCpl(context: Context,detailUrl:String){
//        PlayMeUtil.openAdDetail(context, Config.CPL_ID, detailUrl)
        PlayMeUtil.openAdDetail(context, Config.CPL_CID,  mUserId,  detailUrl,  mImei,  mDeviceOaid,  Config.CPL_CPL_KEY,Config.CPL_APP_ID,"酷爱玩")
    }


    fun toPage(context: Context,pathUrl:String?){
        if (pathUrl.isNullOrBlank()) return
        if (pathUrl.startsWith("http")|| pathUrl.startsWith("https")){
            CommonWebviewActivity.start(context,pathUrl)
        }else{
            val params = pathUrl.substring(pagePre.length).split("&")
            val finalParam = hashMapOf<String,String>()
            params.forEach {
                val split = it.split("=")
                if (split.size > 1){
                    finalParam[split[0]] = split[1]
                }
            }
            val pageIndex = finalParam["Page"]
            when (pageIndex?.toIntOrNull()) {
                1 -> {//首页 KU_WAN_PAGE_INDEX/?Page=1
                    if (context is MainActivity){
                        context.switchTab(MainActivity.EXTRA_TAB_HOME)
                    }else{
                        MainActivity.start(context,MainActivity.EXTRA_TAB_HOME)
                    }
                }
                2 -> {//赚钱 KU_WAN_PAGE_INDEX/?Page=2
                    if (context is MainActivity){
                        context.switchTab(MainActivity.EXTRA_TAB_EARN)
                    }else{
                        MainActivity.start(context,MainActivity.EXTRA_TAB_EARN)
                    }
                }
                3 -> {//邀请 KU_WAN_PAGE_INDEX/?Page=3
                    if (context is MainActivity){
                        context.switchTab(MainActivity.EXTRA_TAB_INVITE)
                    }else{
                        MainActivity.start(context,MainActivity.EXTRA_TAB_INVITE)
                    }
                }
                4 -> {//福利 KU_WAN_PAGE_INDEX/?Page=4
                    if (context is MainActivity){
                        context.switchTab(MainActivity.EXTRA_TAB_WELFARE)
                    }else{
                        MainActivity.start(context,MainActivity.EXTRA_TAB_WELFARE)
                    }
                }
                5 -> {//激励视频
                    openRewardVideo(context)
                }
                6 -> {//小视频
                    VideoActivity.start(context)
                }
                7 -> {//客服
                    ServiceHelper.open(context)
                }
                8 -> {//设置
                    SettingActivity.start(context)
                }
                9 -> {//消息
                    MessageActivity.start(context)
                }
                10 -> {//提现记录
                    WithdrawRecordActivity.start(context)
                }
                11 -> {//我的任务
                    MyTaskActivity.start(context)
                }
                12 -> {//我的小游戏
                    MySmallGameActivity.start(context)
                }

            }
        }
    }

    /**
     * 穿山甲激励视频
     */
    private fun openRewardVideo(context: Context) {
        getAdPlatform(AdPlatformType.CSJ)?.loadAd(context, AdSlotType.AD_VIDEO,Constants.NATIVE_AD_CODE,true,"")
    }

    /**
     * 获取广告平台实例
     * @param type
     * @see [cn.yanhu.kuwanapp.welfare.ad.AdPlatformType]
     */
    private fun getAdPlatform(type: Int): IAdProcess? {
        val adPlatformInstance = AdPlatformFactory.getAdPlatformInstance(type)
        adPlatformInstance?.mAdLoadLifecycle = object : SimpleAdLifecycle() {
            override fun adLoadFail(innerCodeId: String, msgError: String) {
                showToast(msgError)
            }

            override fun adPlayComplete(innerCodeId: String) {

            }

            override fun adDismissed(innerCodeId: String) {
                LiveEventBus.get(VideoRewardEvent::class.java).post(VideoRewardEvent(source = VideoRewardEvent.SOURCE_HOME,businessType = "2"))
            }
        }
        return adPlatformInstance
    }
}